-
-
Notifications
You must be signed in to change notification settings - Fork 125
fix(delegate): simulate cascade delete #2120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 Walkthrough""" WalkthroughThe changes introduce support for cascade deletion in polymorphic model hierarchies. New metadata properties for relation actions were added, the delegate deletion logic was updated to recursively cascade deletes to related entities and their base models, and integration tests were expanded to verify correct cascade deletion behavior across model inheritance. Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant DelegateProxyHandler
participant CrudContract
participant RelatedDelegate
participant BaseDelegate
Client->>DelegateProxyHandler: delete(entity)
DelegateProxyHandler->>CrudContract: injectWhereAndSelect()
DelegateProxyHandler->>DelegateProxyHandler: getRelationDelegateEntitiesForCascadeDelete()
alt Related entities with Cascade
loop For each related entity
DelegateProxyHandler->>DelegateProxyHandler: doDelete(related entity, readBack=false)
end
end
DelegateProxyHandler->>CrudContract: delete main entity
alt Need to delete base entity
DelegateProxyHandler->>DelegateProxyHandler: doDelete(base entity, readBack=false)
end
DelegateProxyHandler-->>Client: return result
Assessment against linked issues
Note ⚡️ AI Code Reviews for VS Code, Cursor, WindsurfCodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback. Note ⚡️ Faster reviews with cachingCodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure 📜 Recent review detailsConfiguration used: CodeRabbit UI 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
⏰ Context from checks skipped due to timeout of 90000ms (6)
✨ Finishing Touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 2
🧹 Nitpick comments (2)
packages/runtime/src/enhancements/node/delegate.ts (2)
1227-1236: Inefficient N × M querying – combine look-upsFor every qualifying relation field the code performs a standalone
findMany, which explodes to N database round-trips per parent delete.
When deleting a batch of parents (deleteMany→doDeleteMany→ per-entitydoDelete) this balloons to N × M queries.Consider aggregating the look-ups:
- Collect all relation models in one pass.
- Issue a single
findManyper relation model with aninfilter on the compound ids of the parent set.This reduces DB chatter and speeds up cascade deletes on large data sets.
[performance]
1243-1245: Hard-coded literal'Cascade'– favour enum safety
onDeleteActionis typed asRelationAction; hard-coding the string makes the comparison brittle to future enum renames or casing changes.-return fieldInfo.onDeleteAction === 'Cascade'; +return fieldInfo.onDeleteAction === 'Cascade'; // TODO: replace with constant e.g., RelationAction.CascadeExport a constant (or reuse Prisma’s) so the compiler flags typos automatically.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting
📒 Files selected for processing (4)
packages/runtime/src/cross/model-meta.ts(2 hunks)packages/runtime/src/enhancements/node/delegate.ts(1 hunks)packages/sdk/src/model-meta-generator.ts(2 hunks)tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts(2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (3)
tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts (1)
packages/testtools/src/schema.ts (1)
loadSchema(163-369)
packages/sdk/src/model-meta-generator.ts (1)
packages/sdk/src/utils.ts (3)
getAttribute(141-157)getAttributeArg(172-182)isEnumFieldReference(196-198)
packages/runtime/src/enhancements/node/delegate.ts (3)
packages/runtime/src/types.ts (1)
CrudContract(86-86)packages/runtime/src/enhancements/node/utils.ts (1)
formatObject(9-11)packages/runtime/src/cross/model-meta.ts (2)
getFields(237-239)FieldInfo(31-122)
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: dependency-review
- GitHub Check: OSSAR-Scan
🔇 Additional comments (7)
packages/runtime/src/cross/model-meta.ts (2)
23-26: Good addition of the RelationAction typeThe new
RelationActiontype correctly defines all standard Prisma relation action values for cascade behaviors.
82-90: Appropriate metadata extension for relation actionsThe addition of
onDeleteActionandonUpdateActionproperties to theFieldInfotype correctly enables tracking relation behaviors at runtime. This is essential for implementing cascade operations across the model hierarchy.packages/sdk/src/model-meta-generator.ts (3)
314-325: Well implemented extraction of relation actionsThe code correctly extracts
onDeleteActionandonUpdateActionvalues from model fields and adds them to the generated metadata. This enables the runtime to properly handle cascading operations.
584-593: Good extraction of onDelete relation attributeThe
getOnDeleteActionhelper function properly retrieves theonDeleteargument from the@relationattribute if it exists and is an enum reference.
595-604: Good extraction of onUpdate relation attributeThe
getOnUpdateActionhelper function properly retrieves theonUpdateargument from the@relationattribute if it exists and is an enum reference. This complements theonDeleteActionsupport for complete relation action handling.tests/integration/tests/enhancements/with-delegate/enhanced-client.test.ts (2)
1060-1060: Appropriate test renamingRenaming the test from
deletetodelete simpleimproves clarity and differentiates it from the new cascade delete test.
1109-1156: Comprehensive cascade delete test implementationThe new test case thoroughly validates the cascade delete functionality across a polymorphic model hierarchy. It:
- Creates a schema with proper inheritance relationships
- Establishes
onDelete: Cascaderelations between models- Creates nested records and verifies that deleting the parent cascades to children
- Checks both the enhanced client and the underlying Prisma client for proper deletion
This test effectively verifies that cascade delete works as expected through delegate enhancements.
fixes #2102